﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace TravelexForwardContracts
{
    public partial class NewContractsForm : Form
    {
        /* **************************************************
         * Data Member
         * **************************************************/
        DataSetPayment m_dsPayment;
        DataSetAllFEC m_dsAllFEC;


        public NewContractsForm()
        {
            InitializeComponent();
            m_dsPayment = new DataSetPayment();
            m_dsAllFEC = new DataSetAllFEC();

        }

        /* *************************************************
         * Private Functions
         * *************************************************/
        private void paymentBrowse_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog.DefaultExt = "xls";
                openFileDialog.Filter = "Excel File: *.xls|*.xls";
                openFileDialog.FileName = "";
                DialogResult tmpRlt = openFileDialog.ShowDialog();

                if (tmpRlt == DialogResult.OK)
                {
                    paymentTextBox.Text = openFileDialog.FileName;
                }
            }
            catch (System.Exception ex)
            {
                using (HMessageBox hMsg = new HMessageBox())
                {
                    hMsg.logMsg("[NG] Browse Paid & Unpaid Excel File", ex.Message);
                }
            }
        }

        private void allFecBrowse_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog.DefaultExt = "xls";
                openFileDialog.Filter = "Excel File: *.xls|*.xls";
                openFileDialog.FileName = "";
                DialogResult tmpRlt = openFileDialog.ShowDialog();

                if (tmpRlt == DialogResult.OK)
                {
                    allFecTextBox.Text = openFileDialog.FileName;
                }
            }
            catch (System.Exception ex)
            {
                using (HMessageBox hMsg = new HMessageBox())
                {
                    hMsg.logMsg("[NG] Browse All FEC Excel File", ex.Message);
                }
            }
        }

        /*
         * @brief : 1. process paid&unpaid.xls
         *          2. process All FEC.xls
         *          3. Find Out New Contracts
         *          4. Find Out Drop Out Contracts
         */
        private void processButton_Click(object sender, EventArgs e)
        {
            // Check File Validation 
            if (paymentTextBox.Text == "")
            {
                MessageBox.Show("NG: No Paid & Unpaid file specified", "Warning Message",
                    System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                return;
            }

            if (allFecTextBox.Text == "")
            {
                MessageBox.Show("NG: No All FEC file specified", "Warning Message",
                    System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                return;
            }

            if (UpdateDateTimePicker.Value <= PreviousDateTimePicker.Value)
            {
                MessageBox.Show("Error:'Previous Date' cannot beyond 'Update Date'", "Error Msg",
                    System.Windows.Forms.MessageBoxButtons.OK,
                    System.Windows.Forms.MessageBoxIcon.Warning);
                return;
            }

            // Process Paid & Unpaid .xls ->Output --> DataSetPayment
            processButton.Enabled = false;

            XPaymentExcel xPayment = new XPaymentExcel();
            m_dsPayment = xPayment.processExcel(paymentTextBox.Text.Trim(), 
                UpdateDateTimePicker.Value);
            if (m_dsPayment != null)
            {
                PaymentDataGridView.DataSource = m_dsPayment;
                PaymentDataGridView.DataMember = m_dsPayment.Tables[0].TableName;
            }
            else
            {
                MessageBox.Show("Processing Paid & Unpaid Failed", "Error Message",
                    System.Windows.Forms.MessageBoxButtons.OK,
                    System.Windows.Forms.MessageBoxIcon.Error);
                return;
            }

            // Process All FEC.xls --> Output --> DataSetAllFEC
            XAllFECExcel xAllFEC = new XAllFECExcel();
            m_dsAllFEC = xAllFEC.processExcel(allFecTextBox.Text.Trim(), UpdateDateTimePicker.Value);
            if (m_dsAllFEC != null)
            {
                AllFECDataGridView.DataSource = m_dsAllFEC;
                AllFECDataGridView.DataMember = m_dsAllFEC.Tables[0].TableName;
            }
            else
            {
                MessageBox.Show("Processing All FEC.xls Failed", "Error Message",
                    System.Windows.Forms.MessageBoxButtons.OK,
                    System.Windows.Forms.MessageBoxIcon.Error);
                return;
            }

            // New Contract (previousDate doesn't has, UpdateDate has --> New Record)
            XDBOp xDb = new XDBOp();
            DataSetPayment dsNewContracts = new DataSetPayment();
            dsNewContracts = xDb.findNewContracts(m_dsPayment, PreviousDateTimePicker.Value);
            if (dsNewContracts != null && dsNewContracts.Tables[0].Rows.Count > 0)
            {
                NewContractDataGridView.DataSource = dsNewContracts;
                NewContractDataGridView.DataMember = dsNewContracts.Tables[0].TableName;
            }
            else
            {
                NewLabel.Text = "Result: There is no New Contract be found";
            }
   
            // Drop Out Contract (previousDate has, UpdateDate doesn't has -->Drop Records
            DataSetPayment dsDropOutContracts = new DataSetPayment();
            dsDropOutContracts = xDb.findDropOutContracts(m_dsPayment, PreviousDateTimePicker.Value);
            if (dsDropOutContracts != null && dsDropOutContracts.Tables[0].Rows.Count > 0)
            {
                DropOutDataGridView.DataSource = dsDropOutContracts;
                DropOutDataGridView.DataMember = dsDropOutContracts.Tables[0].TableName;
            }
            else
            {
                DropOutLabel.Text = "Result: There is no Dropped Out Contract be found";
            }
            UpdateDatabaseLinkLabel.Enabled = true;
        }

        private void NewContractsForm_Load(object sender, EventArgs e)
        {
            PreviousDateTimePicker.Value = DateTime.Now.AddDays(-1);
            UpdateDateTimePicker.Value = DateTime.Now;
            UpdateDatabaseLinkLabel.Enabled = false;
        }

        private void ClearButton_Click(object sender, EventArgs e)
        {
            PaymentDataGridView.DataSource = null;
            AllFECDataGridView.DataSource = null;
            NewLabel.Text = "";
            DropOutLabel.Text = "";
            NewContractDataGridView.DataSource = null;
            DropOutDataGridView.DataSource = null;
            processButton.Enabled = true;
            UpdateDatabaseLinkLabel.Enabled = false;
        }

        private void UpdateDatabaseLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (m_dsPayment == null || m_dsPayment.Tables[0].Rows.Count < 1)
            {
                MessageBox.Show("[MSG]Nothing Update for Paid&Unpaid.xls", "Message",
                    System.Windows.Forms.MessageBoxButtons.OK,
                    System.Windows.Forms.MessageBoxIcon.Asterisk);
                return;
            }

            UpdateDatabaseLinkLabel.Enabled = false;

            if (m_dsAllFEC == null || m_dsAllFEC.Tables[0].Rows.Count < 1)
            {
                MessageBox.Show("[MSG]Nothing Update for All FEC.xls", "Message",
                    System.Windows.Forms.MessageBoxButtons.OK,
                    System.Windows.Forms.MessageBoxIcon.Asterisk);
                return;
            }

            XDBOp xDb = new XDBOp();
            xDb.updatePayment(m_dsPayment);
            xDb.updateAllFEC(m_dsAllFEC);

            MessageBox.Show("Update Over", "Message", System.Windows.Forms.MessageBoxButtons.OK,
                System.Windows.Forms.MessageBoxIcon.Asterisk);
            UpdateDatabaseLinkLabel.Enabled = false;
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
